/**
 * 
 */
package net.phoenix.repository.observation;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.observation.EventListener;

/**
 * @author lixf
 * 
 */
public class EventListenerWrapper {
	private EventListener listener;
	private int eventTypes;
	private String absPath;
	private boolean isDeep;
	private String[] uuid;
	private String[] nodeTypeName;
	private boolean noLocal;

	public EventListenerWrapper(EventListener listener, int eventTypes, String absPath, boolean isDeep, String[] uuid, String[] nodeTypeName, boolean noLocal) {
		this.listener = listener;
		this.eventTypes = eventTypes;
		this.absPath = absPath;
		this.isDeep = isDeep;
		this.uuid = uuid;
		this.nodeTypeName = nodeTypeName;
		this.noLocal = noLocal;
	}

	/**
	 * @param node
	 * @param type
	 * @return
	 * @throws RepositoryException
	 */
	public boolean applyTo(Node node, int type) throws RepositoryException {
		boolean apply = false;
		if (!apply)
			apply = ((type & eventTypes) > 0);
		if (!apply && absPath != null) {
			if (isDeep)
				apply = node.getPath().startsWith(absPath);
			else
				apply = node.getPath().equals(absPath);
		}
		if (!apply && (uuid != null)) {
			String path = node.getPath();
			for (int i = 0; i < uuid.length && !apply; i++) {
				Node parent = node.getSession().getNodeByIdentifier(uuid[i]);
				if (path.startsWith(parent.getPath()))
					apply = true;
			}
		}
		if (!apply && (nodeTypeName != null)) {
			for (int i = 0; i < nodeTypeName.length && !apply; i++) {
				if (node.isNodeType(nodeTypeName[i]))
					apply = true;
			}
		}
		return apply;
	}
	/**
	 * @param property
	 * @param type
	 * @return
	 * @throws RepositoryException
	 */
	public boolean applyTo(Property property, int type) throws RepositoryException {
		boolean apply = false;
		if (!apply)
			apply = ((type & eventTypes) > 0);
		if (!apply && absPath != null) {
			if (isDeep)
				apply = property.getPath().startsWith(absPath);
			else
				apply = property.getPath().equals(absPath);
		}
		if (!apply && (uuid != null)) {
			String path = property.getPath();
			for (int i = 0; i < uuid.length && !apply; i++) {
				Node parent = property.getSession().getNodeByIdentifier(uuid[i]);
				if (path.startsWith(parent.getPath()))
					apply = true;
			}
		}
		Node node = property.getNode();
		if (!apply && (nodeTypeName != null)) {
			for (int i = 0; i < nodeTypeName.length && !apply; i++) {
				if (node.isNodeType(nodeTypeName[i]))
					apply = true;
			}
		}
		return apply;
	}
	public EventListener getListener() {
		return listener;
	}

	public int getEventTypes() {
		return eventTypes;
	}

	public String getAbsPath() {
		return absPath;
	}

	public boolean isDeep() {
		return isDeep;
	}

	public String[] getUuid() {
		return uuid;
	}

	public String[] getNodeTypeName() {
		return nodeTypeName;
	}

	public boolean isNoLocal() {
		return noLocal;
	}

}
